﻿// (c) Copyright 2013 Mike Riedel.
// This source is subject to the Microsoft Public License (Ms-PL).
// All other rights reserved.

using System;
using System.Threading.Tasks;

namespace FreshMeat.Threading
{
   /// <summary>
   /// Provides a lock that doesn't actually do anything.
   /// Useful for testing or perhaps bypassing locking in situations where it
   /// isn't useful without changing code that was written to always use locking.
   /// </summary>
   public class NullLock : ILock
   {
      /// <summary>
      /// Instance of a null lock to avoid having to create any more than necessary.
      /// </summary>
      public static readonly NullLock Instance = new NullLock();

      readonly Task<object> nothing = TaskEx.FromResult<object>(null);

      /// <summary>
      /// Returns immediately; does nothing.
      /// </summary>
      public void Wait()
      {
      }

      /// <summary>
      /// Returns immediately; does nothing.
      /// </summary>
      /// <returns></returns>
      public Task WaitAsync()
      {
         return nothing;
      }

      /// <summary>
      /// Returns immediately; does nothing.
      /// </summary>
      public void Done()
      {
      }

      /// <summary>
      /// Gets a disposable context that does nothing.
      /// </summary>
      /// <returns></returns>
      public IDisposable Context()
      {
         return LockContext.Nothing;
      }
   }
}